import { defineStore } from 'pinia'

interface Notification {
  type: 'order' | 'stock' | 'system'
  content: string
  timestamp: number
  read?: boolean
}

export const useNotificationStore = defineStore('notification', {
  state: () => ({
    notifications: [] as Notification[],
    unreadCount: 0,
  }),
  actions: {
    addNotification(notification: Notification) {
      this.notifications.unshift({ ...notification, read: false })
      this.unreadCount++
    },
    markAsRead(index: number) {
      if (!this.notifications[index]?.read) {
        this.notifications[index].read = true
        this.unreadCount = Math.max(0, this.unreadCount - 1)
      }
    },
    removeNotification(index: number) {
      if (!this.notifications[index]?.read) {
        this.unreadCount = Math.max(0, this.unreadCount - 1)
      }
      this.notifications.splice(index, 1)
    },
    markAllAsRead() {
      this.unreadCount = 0
      this.notifications = this.notifications.map((n) => ({ ...n, read: true }))
    },
    removeAll() {
      this.notifications = []
      this.unreadCount = 0
    },
  },
})
